package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.AftersaleTransferRecord;
import com.rfid.project.service.AftersaleTransferRecordService;
import com.rfid.project.model.People;
import com.rfid.project.service.PeopleService;
import com.rfid.project.model.AftersaleRecord;
import com.rfid.project.service.AftersaleRecordService;

@Controller
public class AftersaleTransferRecordController {

	private static final Logger logger = LoggerFactory
			.getLogger(AftersaleTransferRecordController.class);

	@Autowired
	private AftersaleTransferRecordService aftersaleTransferRecordService;

	@Autowired
	private PeopleService peopleService;

	@Autowired
	private AftersaleRecordService aftersaleRecordService;
	
	@RequestMapping(value = "/aftersaleTransferRecordmgr")
	public String inboxmgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<People>peoples = peopleService.findAll();
		model.addAttribute("peoples", peoples);
		return "aftersaleTransferRecordmgr";
	}

	@RequestMapping(value = "/queryAftersaleTransferRecord")
	public String queryAftersaleTransferRecord(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "remark", defaultValue = "") String remark,		
			@RequestParam(value = "transferTimeBegin", defaultValue = "") Date transferTimeBegin,
			@RequestParam(value = "transferTimeEnd", defaultValue = "") Date transferTimeEnd,
			@RequestParam(value = "aftersaleRecordId", defaultValue = "-1") Long aftersaleRecordId,
			@RequestParam(value = "originalHandlePersonId", defaultValue = "-1") Long originalHandlePersonId,
			@RequestParam(value = "targetHandlePersonId", defaultValue = "-1") Long targetHandlePersonId,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<AftersaleTransferRecord> aftersaleTransferRecords = aftersaleTransferRecordService.findAll(originalHandlePersonId, targetHandlePersonId, aftersaleRecordId, transferTimeBegin, transferTimeEnd, remark, pageNumber, pageSize, sort, order);
				
		JSONObject object = new JSONObject();
		object.put("total", aftersaleTransferRecords.getTotalElements());
		JSONArray models = new JSONArray();
		for (AftersaleTransferRecord item : aftersaleTransferRecords) {
			JSONObject mod = new JSONObject();
			
			mod.put("id", item.getId());
			mod.put("transferTime", com.rfid.project.util.DateUtil
					.dateAllToString(item.getTransferTime()));
			mod.put("remark", item.getRemark());
			
			People originalHandlePerson_1 = item.getOriginalHandlePerson();
			if(null!=originalHandlePerson_1){
			mod.put("originalHandlePersonId", item.getOriginalHandlePerson().getId());
			mod.put("originalHandlePersonName", item.getOriginalHandlePerson().getName());}
			else
			{
				mod.put("originalHandlePersonId", "-1");
				mod.put("originalHandlePersonName", "");				
			}
			
			People targetHandlePerson_1 = item.getTargetHandlePerson();
			if(null!=targetHandlePerson_1){
			mod.put("targetHandlePersonId", item.getTargetHandlePerson().getId());
			mod.put("targetHandlePersonName", item.getTargetHandlePerson().getName());}
			else
			{
				mod.put("targetHandlePersonId", "-1");
				mod.put("targetHandlePersonName", "");				
			}
						
			AftersaleRecord aftersaleRecord_1 = item.getAftersaleRecord();
			if(null!=aftersaleRecord_1){
			mod.put("aftersaleRecordId", item.getAftersaleRecord().getId());
			mod.put("IssueDescription", item.getAftersaleRecord().getIssueDescription());}
			else
			{
				mod.put("aftersaleRecordId", "-1");
				mod.put("IssueDescription", "");				
			}
			
			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}

	@RequestMapping(value = "/editAftersaleTransferRecord", method = RequestMethod.POST)
	public String editInbox(HttpServletRequest request,
			HttpServletResponse response, @Valid AftersaleTransferRecord aftersaleTransferRecord,
			Model model, RedirectAttributes redirectAttributes) {
		aftersaleTransferRecordService.saveAftersaleTransferRecord(aftersaleTransferRecord);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delAftersaleTransferRecord")
	public String delAftersaleRecordWorktime(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.aftersaleTransferRecordService.deleteAftersaleTransferRecord(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {

		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}
